﻿using System;
using System.Collections;
using System.Text;
 
namespace SpeakerRecognition
{
    /// <summary>
    /// Class for averaging overlapping frames into one double array 
    /// </summary>
    public class AverageFramesCommand : ICommand
    {
        //************************  Private Fields  *******************************// 
        private double[][] inputData;
        private double[] outputData;
        private int overlapSize;       
        //************************  Public Properties  ****************************// 
        public double[][] InputData
        {
            get { return inputData; }
            set { inputData = value; }
        }
        public double[] OutputData
        {
            get { return outputData; }
            set { outputData = value; }
        }
        public int OverlapSize
        {
            get { return overlapSize; }
            set { overlapSize = value; }
        }
        //************************  Public Methods  ****************************// 
        #region ICommand Members

        public void execute()
        {
                // array list to save the data after calculating averaging
            ArrayList temp = new ArrayList();
            int winSize = inputData[0].Length ;
                // loop for the first part without the overlap 
            for (int i = 0; i < winSize- overlapSize;i++ )
            {
                temp.Add(inputData[0][i]);
            }
                //loop to calculate the averages for each overlap part
            for (int j = 1; j < inputData.Length;j++)
            {
               
                double avg;
                for (int lower = 0; lower < overlapSize; lower++)
                {
                    
                    avg = (inputData[j][lower]+inputData[j-1][winSize-overlapSize+lower])/2;
                    temp.Add(avg);
                }
                for (int t = overlapSize; t < winSize - overlapSize; t++)
                {
                    temp.Add(inputData[j][t]);
                }
            }
                // loop for the final part without the overlap 
            for (int t = winSize - overlapSize; t < winSize; t++)
            {
                temp.Add(inputData[inputData.Length - 1][t]);
            }

                // convert the array list to double array for output
            outputData = new double[temp.Count];
            for (int j = 0; j < outputData.Length; j++)
            {
                outputData[j] = (double)temp[j];
            }

        }

        #endregion
    }
}
